package anyVehicle;

import net.minecraft.src.AxisAlignedBB;
import net.minecraft.src.Vec3D;

import org.lwjgl.util.vector.Vector4f;

public class VehicleBlockBoundingBox extends AxisAlignedBB {
	private double yaw = 0D;
	private double pitch = 0D;
	private Vehicle vehicle;
	private Vector4f parentCorner1;
	private Vector4f parentCorner2;
	private Vector4f parentCorner3;
	private Vector4f parentCorner4;
	private Vector4f parentCorner5;
	private Vector4f parentCorner6;
	private Vector4f parentCorner7;
	private Vector4f parentCorner8;
	private double calculatedXOffset;
	private double calculatedYOffset;
	private double calculatedZOffset;

	public VehicleBlockBoundingBox(Vehicle vehicle, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
		super(minX, minY, minZ, maxX, maxY, maxZ);
		this.vehicle = vehicle;

	}
	
	
	public VehicleBlockBoundingBox(Vehicle vehicle, AxisAlignedBB bb) {
		super(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.maxY, bb.maxZ);
		this.vehicle = vehicle;

	}

	@Override
	public boolean intersectsWith(AxisAlignedBB bb) {
		calculateParentCorners();
		// FIXME This does not work well for all cases. Sometimes cube vertexes are not inside the other cube, but they still collide
		return bb.isVecInside(Vec3D.createVector(parentCorner1.x,
				parentCorner1.y, parentCorner1.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner2.x,
						parentCorner2.y, parentCorner2.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner3.x,
						parentCorner3.y, parentCorner3.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner4.x,
						parentCorner4.y, parentCorner4.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner5.x,
						parentCorner5.y, parentCorner5.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner6.x,
						parentCorner6.y, parentCorner6.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner7.x,
						parentCorner7.y, parentCorner7.z))
				|| bb.isVecInside(Vec3D.createVector(parentCorner8.x,
						parentCorner8.y, parentCorner8.z));
	}

	public void calculateParentCorners() {
		Vector4f corner1 = new Vector4f((float) minX, (float) minY,
				(float) minZ, 1F);
		Vector4f corner2 = new Vector4f((float) minX, (float) minY,
				(float) maxZ, 1F);
		Vector4f corner3 = new Vector4f((float) minX, (float) maxY,
				(float) minZ, 1F);
		Vector4f corner4 = new Vector4f((float) minX, (float) maxY,
				(float) maxZ, 1F);
		Vector4f corner5 = new Vector4f((float) maxX, (float) minY,
				(float) minZ, 1F);
		Vector4f corner6 = new Vector4f((float) maxX, (float) minY,
				(float) maxZ, 1F);
		Vector4f corner7 = new Vector4f((float) maxX, (float) maxY,
				(float) minZ, 1F);
		Vector4f corner8 = new Vector4f((float) maxX, (float) maxY,
				(float) maxZ, 1F);

		parentCorner1 = vehicle.localPosToParent(corner1);
		parentCorner2 = vehicle.localPosToParent(corner2);
		parentCorner3 = vehicle.localPosToParent(corner3);
		parentCorner4 = vehicle.localPosToParent(corner4);
		parentCorner5 = vehicle.localPosToParent(corner5);
		parentCorner6 = vehicle.localPosToParent(corner6);
		parentCorner7 = vehicle.localPosToParent(corner7);
		parentCorner8 = vehicle.localPosToParent(corner8);
	}

	@Override
	public double calculateXOffset(AxisAlignedBB axisalignedbb, double d) {
		return calculatedXOffset;
		

	}

	@Override
	public double calculateYOffset(AxisAlignedBB axisalignedbb, double d) {
		return calculatedYOffset;
	}

	@Override
	public double calculateZOffset(AxisAlignedBB axisalignedbb, double d) {
		return calculatedZOffset;
	}


	public void calculateOffsets(AxisAlignedBB bb, double dx, double dy, double dz) {
		double bbX = (bb.maxX + bb.minX) /2D;
		double bbY = (bb.maxY + bb.minY) /2D;
		double bbZ = (bb.maxZ + bb.minZ) /2D;
		
		// http://mathworld.wolfram.com/Point-PlaneDistance.html
		
		
		
	}

}
